<template>
  <el-form
    v-loading="loading"
    ref="form"
    :model="form"
    label-width="150px"
  >
    <el-form-item
      label="保险费用："
      :rules="[{required: true, message: '请输入订单服务费'}]"
    >
      <el-input
        size="small"
        v-model="form.insurance"
        placeholder="请输入保险费用"
      ></el-input>
    </el-form-item>
    <el-form-item
      label="订单服务费："
      prop="order"
      :rules="[{required: true, message: '请输入订单服务费'}]"
    >
      <el-input
        size="small"
        v-model="form.order"
        placeholder="请输入订单服务费"
      ></el-input>
    </el-form-item>
    <el-form-item>
      <el-button
        size="small"
        type="primary"
        :loading="submit_loading"
        @click="ensureEdit"
      >
        确定
      </el-button>
    </el-form-item>
  </el-form>
</template>

<script>
  /**
   * @Author: lifuzhao
   * @Date: 2019-07-18
   * @Project: driver
   */
  export default {
    name: "index"
    , data() {
      return {
        form: {
          insurance: ''
          , order: ''
        }
        , loading: false
        , submit_loading: false
      }
    }
    , computed: {
      url() {
        return this.form.id && this.form.id > 0 ? '/api/v1/SystemPrice/order/update' : '/api/v1/SystemPrice/order/save'
      }
    }
    , mounted() {
      this.getConfig()
    }
    , methods: {
      getConfig() {
        this.loading = true
        this.$axios.get('/api/v1/SystemPrice/order')
          .then(res => {
            let {success, ...data} = res
            if (success) {
              Object.keys(data).forEach(key => {
                this.$set(this.form, key, data[key])
              })
            }
          })
          .catch(() => {

          })
          .finally(() => {
            this.loading = false
          })
      }
      , ensureEdit() {
        this.$refs.form.validate(valid => {
          if (!valid) return
          this.submit_loading = true
          this.$axios.post(this.url, this.form)
            .then(res => {
              let {success, msg} = res
              if (success) {
                this.$message({
                  message: '更新成功'
                  , type: 'success'
                })
              } else {
                this.$message({
                  message: '更新失败' + msg
                  , type: 'error'
                })
              }
            })
            .catch(() => {
              this.$message({
                message: '更新失败'
                , type: 'error'
              })
            })
            .finally(() => {
              this.submit_loading = false
            })
        })
      }
    }
  }
</script>

<style scoped>

</style>